﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ZG_member.BLL;
using System.Security.Cryptography;
using System.Configuration;
namespace ZG_member
{
    public partial class changepassword : Form
    {
        public changepassword()
        {
            InitializeComponent();
            this.Opacity = 0;
            skinEngine1.SkinFile = Application.StartupPath + ConfigurationManager.AppSettings["Skin"];
        }

       
        private void btnOk_Click(object sender, EventArgs e)
        {
            if (txtCardId.Text == "")
            {
                return;
            }
            if (txtmemberName.Text == "")
            {
                return;
            }
            
            memberInfo info = new memberInfo();
            Model.memberInfo infoModel = new Model.memberInfo();          
            //infoModel = info.GetModelList("memberCardId='"+txtCardId.Text+"'")[0];
            infoModel = info.GetModels(this.txtCardId.Text);
            if (infoModel.pwd == null)
            {
                if (txtnewpassword.Text != txtnewpassword1.Text)
            {
                MessageBox.Show("两次密码输入不一致!"); return;
            }
            MD5 md = new MD5CryptoServiceProvider();
            byte[] btt = Encoding.Default.GetBytes(txtnewpassword.Text);
            btt = md.ComputeHash(btt);
            string newpwd = BitConverter.ToString(btt).Replace("-", "");
            infoModel.pwd = newpwd;
           
            if (info.Update(infoModel))
            {
                MessageBox.Show("修改成功");
                this.Close();
            }
            }
            else
            {
                MD5 md5 = new MD5CryptoServiceProvider();
                byte[] bt = Encoding.Default.GetBytes(txtoldpassword.Text);
                bt = md5.ComputeHash(bt);
                string oldpwd = BitConverter.ToString(bt).Replace("-", "");

                if (oldpwd != infoModel.pwd)
                {
                    MessageBox.Show("旧密码输入错误!"); return;
                }
                if (txtnewpassword.Text != txtnewpassword1.Text)
                {
                    MessageBox.Show("两次密码输入不一致!"); return;
                }
                MD5 md = new MD5CryptoServiceProvider();
                byte[] btt = Encoding.Default.GetBytes(txtnewpassword.Text);
                btt = md.ComputeHash(btt);
                string newpwd = BitConverter.ToString(btt).Replace("-", "");
                infoModel.pwd = newpwd;

                if (info.Update(infoModel))
                {
                    MessageBox.Show("修改成功");
                    this.Close();
                }
            }


            
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.timer2.Start();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            this.Left += 2;
            this.Opacity += 0.1;
            if (Opacity == 1)
            {
                this.timer1.Stop();

            }
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            this.Top = this.Top + 10;
            this.Opacity = this.Opacity - 0.06;
            if (Opacity <= 0)
            {
                this.timer2.Stop();
                this.Close();
            }
        }

        private void changepassword_Load(object sender, EventArgs e)
        {
            this.timer1.Start();
        }
        int flag = 0;
        private void changepassword_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (flag == 0)
            {
                e.Cancel = true;
                flag = 1;
                this.timer2.Start();
            }
        }

        private void txtCardId_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(((e.KeyChar >= '0') && (e.KeyChar <= '9')) || e.KeyChar <= 31))
            {
                //if (e.KeyChar == '.')
                //{
                //    if (((TextBox)sender).Text.Trim().IndexOf('.') > -1)
                //        e.Handled = true;
                //}
                //else
                e.Handled = true;
            }
            else
            {
                if (e.KeyChar <= 31)
                {
                    e.Handled = false;
                }
                else if (((TextBox)sender).Text.Trim().IndexOf('.') > -1)
                {
                    if (((TextBox)sender).Text.Trim().Substring(((TextBox)sender).Text.Trim().IndexOf('.') + 1).Length >= 4)
                        e.Handled = true;
                }
                if (e.KeyChar <= 31)
                {
                    e.Handled = true;
                }
                
            }
        }
    }
}
